package dt.inlee.common.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

public class ReadWriteFile {
	public static final String HEADER = "#WDDA$,xh,bh-编号,xm-姓名,kh,mm,gl,sr,zw,zp,mj-1";

	// public static final String PATH = "e:/file/test.txt";

	/**
	 * 创建一个文件
	 * 
	 * @param path
	 * @param header
	 * @throws IOException
	 */
	public static void createFile(String path, String header)
			throws IOException {
		FileOutputStream fos;
		PrintWriter pw = null;
		try {
			fos = new FileOutputStream(path, true);
			pw = new PrintWriter(fos);
			pw.write(HEADER + "\r\n");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			pw.flush();
			pw.close();
		}
	}

	/**
	 * 给文件写入内容
	 * 
	 * @param path
	 * @param content
	 */
	public static void writeFile(String path, String content) {
		FileOutputStream fos;
		PrintWriter pw = null;
		try {
			fos = new FileOutputStream(path, true);
			pw = new PrintWriter(fos);
			pw.write(content + "\r\n");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			pw.flush();
			pw.close();
		}
	}

	/**
	 * 清空文件所有内容<br/>
	 * 同时写入标题
	 * 
	 * @param path
	 * @param header
	 */
	public static void clearFile(String path, String header) {
		File file = new File(path);
		RandomAccessFile rf = null;
		FileChannel fc = null;
		try {
			rf = new RandomAccessFile(file, "rw");
			fc = rf.getChannel();
			fc.truncate(0);
			writeFile(path, header);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fc.close();
				rf.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 根据卡号，删除一行数据
	 * 
	 * @param path
	 * @param cardnum
	 */
	public static void delElement(String path, String cardnum) {
		if (cardnum == null) {
			throw new NullPointerException();
		}
		String read;
		FileReader fileread = null;
		File file = new File(path);
		StringBuilder sb = new StringBuilder();
		BufferedReader bufread = null;
		try {
			fileread = new FileReader(file);
			bufread = new BufferedReader(fileread);

			int i = 0;
			while ((read = bufread.readLine()) != null) {
				i++;
//				String[] array = read.split(",");
//				// 当行数为第一行array.length==0或者array[i]的cardNum == cardnum时，不算进来
				if (i == 1) {
					continue;
				}
//				if (array.length > 0) {
//					String cardNum = array[0];
//					if (cardnum.equals(cardNum)) {
//						continue;
//					}
//				}
				if(read.contains(cardnum)){
					continue;
				}
				sb.append(read + "\r\n");
			}
//			sb.delete(sb.length() - 1, sb.length());
			clearFile(path, HEADER);
			writeFile(path, sb.toString());

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fileread.close();
				bufread.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}


	public static void main(String[] args) throws Exception {
		delElement("c:/kq/file/20130823095137.wts", "2223");
	}
}
